import { Context } from 'egg';

export default () => {
    return async function token(ctx: Context, next) {
        const urlArray: string[] = ['/api/wx/employee/searchEmployeeStatusById','/api/wx/employee/valiateEmployee',
        '/api/wx/employee/searchGradeEmployeeList','/api/wx/employee/searchMyScore','/api/wx/employee/searchGradeAndRule',
        '/api/wx/employee/searchGradeEmployeeList','/api/wx/employee/searchEvaluateList','/api/wx/employee/markingEmployee',
        '/api/wx/employee/checkHasProject'];
        const token = ctx.headers.utoken;
        if (token && token.length > 0) {
            const employee_id: any = await ctx.service.employeeService.searchEmployeeByToken(token);
            ctx.request.body.employee_id = employee_id;
        }
        let url = ctx.originalUrl;
        let isNeedToken: boolean = true;
        for (const value of urlArray) {
            if (value === url) {
                await ctx.service.employeeService.checkEmployeeByToken(token);
                break;
            }
        }
        if (!isNeedToken) {
            return;
        } else {
            await next();
        }
    };
};
